<?php


	function menu_links_admin($menus_id = false, $parent_id = 0)
	{
		if (db_result(db_query("SELECT COUNT(1) FROM menu_links WHERE menus_id = $menus_id AND parent_id = $parent_id"), 0) == 0) return;
	?>
	<ul>
	<?php
		$sql = "SELECT * FROM menu_links WHERE menus_id = {$menus_id} AND parent_id = $parent_id ORDER BY sort, id ASC";
		$qry = db_query($sql);
		while ($menu_link = db_fetch_assoc($qry)) {
	?>
		<li>
			<label for="menu_link_<?php echo $menu_link['id']?>_label">
				Label
				<input type="text" name="menu_links[<?php echo $menu_link['id']?>][label]" id="menu_link_<?php echo $menu_link['id']?>_label" value="<?php echo $menu_link['label']?>" />
			</label>
			
			<label for="menu_link_<?php echo $menu_link['id']?>_uri">
				URI
				<input type="text" name="menu_links[<?php echo $menu_link['id']?>][uri]" id="menu_link_<?php echo $menu_link['id']?>_uri" value="<?php echo $menu_link['uri']?>" />
			</label>
			
			<label for="menu_link_<?php echo $menu_link['id']?>_parent_id">
				Parent
				<select name="menu_links[<?php echo $menu_link['id']?>][parent_id]" id="menu_link_<?php echo $menu_link['id']?>_parent_id">
					<option value="0"> - MAIN MENU ITEM - </option>
			<?php
				menu_links_select_options($menus_id, $menu_link['id'], $menu_link['parent_id']);
			?>
				</select>
			</label>
			
			<label for="menu_link_<?php echo $menu_link['id']?>_sort" style="width: 50px;">
				Sort Key
				<input type="text" name="menu_links[<?php echo $menu_link['id']?>][sort]" id="menu_link_<?php echo $menu_link['id']?>_sort" value="<?php echo $menu_link['sort']?>"  style="width: 50px;" />
			</label>
		<?php
			if (authorized('menu_links', 'delete', true)) {
		?> 
			<label for="menu_link_<?php echo $menu_link['id']?>_delete">
				<input type="checkbox" name="menu_links[<?php echo $menu_link['id']?>][delete]" id="menu_link_<?php echo $menu_link['id']?>_delete" value="1" />
				Delete
			</label>
		<?php
			}
		?>
			<?php menu_links_admin($menus_id, $menu_link['id']); ?>
		</li>
	<?php
		}
	?>
	</ul>
	<?php
	}
	
	
	
	function menu_links_select_options($menus_id = false, $menu_links_id = false, $menu_links_id_selected = false, $parent_id = 0, $level = 0)
	{
		if (db_result(db_query("SELECT COUNT(1) FROM menu_links WHERE menus_id = $menus_id AND parent_id = $parent_id"), 0) == 0) return;

		$sql = "SELECT * FROM menu_links WHERE menus_id = {$menus_id} AND parent_id = $parent_id ORDER BY sort, id ASC";
		$qry = db_query($sql);
		while ($menu_link = db_fetch_assoc($qry)) {
			if ($menu_link['id'] == $menu_links_id) $selected = ' disabled="disabled"';
			else $selected = ($menu_link['id'] == $menu_links_id_selected) ? ' selected="selected"' : '';
			$indent = '';
			if ($level > 0) {
				for ($i = 0; $i <= $level; $i++) $indent.= '&nbsp;&nbsp;';
			}
	?>
		<option value="<?php echo $menu_link['id']?>"<?php echo $selected?>><?php echo $indent . $menu_link['label']?></option>
	<?php
			menu_links_select_options($menus_id, $menu_links_id, $menu_links_id_selected, $menu_link['id'], ++$level);
			$level--;
		}
	}
	
	
	
  function message($str)
  {
  	$_SESSION['messages']['messages'][] = $str;
  	return NULL;
  }
  
  
  function messages($array = false)
  {
  	if (empty($array)) {
  		foreach ($array as $msg) message($msg);
  		return true;
  	} else {
  		//count the messages, informations, errors, successes, and warnings; return true if > 0 or false if <= 0
  		return (!empty($_SESSION['messages']));  
  	}
  	
  	return false;
  }
	
	
	
	function meta_tags()
	{
		$t = (!empty($GLOBALS['uri']['title'])) ? $GLOBALS['uri']['title'] : TITLE;
		$k = (!empty($GLOBALS['uri']['keywords'])) ? $GLOBALS['uri']['keywords'] : KEYWORDS;
		$d = (!empty($GLOBALS['uri']['description'])) ? $GLOBALS['uri']['description'] : DESCRIPTION;
	?>
<title><?php echo $t; ?></title>
<meta name="title" content="<?php echo $t; ?>" />
<meta name="keywords" content="<?php echo $k; ?>" />
<meta name="description" content="<?php echo $d; ?>" />
	<?php
	}
	
	
	function module_base($module = false)
	{
		if (empty($module)) $module = path_info();
		$module = trim($module) . '/';
		$module = substr($module, 0, strpos($module, '/'));
		return $module;
	}
	
	
	
	function module_base_from_path($path = false)
	{
  	if (empty($path)) return false;
  	
  	return str_replace(realpath(PHPDRIVER . 'modules/'), '', dirname(realpath($path))); 
  }
	
	
	